package cn.com.ddapp.ddm.ui;

import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.com.ddapp.ddm.AppConfig;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.adapter.LogisticsAdapter;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.Logistics;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import okhttp3.Call;

/**
 * Created by Administrator on 2016/11/4 0004.
 */

public class LogisticsActivity extends BaseActivity {
	@BindView(R.id.listview)
	ListView listView;
	@BindView(R.id.one)
	TextView one;
	@BindView(R.id.two)
	TextView two;
	@BindView(R.id.three)
	TextView three;

	private LogisticsAdapter mAdapter;
	List<Logistics.Logistic> mlist = new ArrayList<>();
	String logisticsNo;
	String orderNo;
	String createTime;

	public static void startIntent(Context context, String logisticsNo,String orderNo,String createTime) {
		Intent intent = new Intent(context, LogisticsActivity.class);
		intent.putExtra("logisticsNo", logisticsNo);
		intent.putExtra("orderNo", orderNo);
		intent.putExtra("createTime", createTime);
		context.startActivity(intent);
	}

	@Override
	public void initView() {
		setTitle("物流查询");
		setTitleLeftImg(R.mipmap.back_icon);
		mAdapter = new LogisticsAdapter(this);
		mAdapter.update(mlist);
		listView.setAdapter(mAdapter);
	}

	@Override
	public void initData() {
		logisticsNo = getIntent().getStringExtra("logisticsNo");
		orderNo = getIntent().getStringExtra("orderNo");
		createTime = getIntent().getStringExtra("createTime");

		one.setText("订单编号："+orderNo);
		two.setText("物流单号："+logisticsNo);
		three.setText("下单时间："+createTime);

		requestData();

	}

	@Override
	public int getContentViewId() {
		return R.layout.activity_logistics;
	}

	@Override
	protected String getFragmentTag() {
		return null;
	}

	private void requestData() {
		final OkUtilCallback okUtilCallback = new OkUtilCallback() {
			@Override
			public void onMyResponse(String response, int id) {
				Logistics entity = gson.fromJson(response, Logistics.class);
				if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult())) && entity.getLogin()) {
				} else if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
					Toast.makeText(LogisticsActivity.this, entity.getResult(), Toast.LENGTH_SHORT).show();
				} else if ((AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
					mlist.addAll(entity.getList());
					mAdapter.notifyDataSetChanged();
				}
			}

			@Override
			public void onError(Call call, Exception e, int id) {
				super.onError(call, e, id);
				BaseApplication.getIntstance().showToast(R.string.network_error, Toast.LENGTH_SHORT, 0, Gravity.CENTER);
			}
		};
		OKUtilAPI.post("http://admin.erp.jxezt.cn//app/orderDelivery/logistics.api", "logisticsNo=" + logisticsNo, okUtilCallback);
	}

}
